/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.portlet.editor.gui.view.settings.panel.portlet;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.genia.toolbox.portlet.editor.gui.PortletEditorGUI;
import com.genia.toolbox.portlet.editor.model.portlet.impl.ContainerPortletModel;
import com.genia.toolbox.portlet.editor.model.portlet.impl.PortletModel;
import com.genia.toolbox.web.portlet.description.ContainerPortletDescription.LayoutDirection;

/**
 * The container settings panel.
 */
@SuppressWarnings("serial")
public class ContainerPortletSettingsPanel
    extends AbstractPortletSettingsPanel
{

  /**
   * The minimum number of sub-portlets label.
   */
  private JLabel labelMin = null;

  /**
   * The minimum number of sub-portlets text field.
   */
  private JTextField textMin = null;

  /**
   * The maximum number of sub-portlets label.
   */
  private JLabel labelMax = null;

  /**
   * The maximum number of sub-portlets text field.
   */
  private JTextField textMax = null;

  /**
   * The direction label.
   */
  private JLabel labelDirection = null;

  /**
   * The direction text field.
   */
  private JComboBox comboDirection = null;



  /**
   * Constructor.
   * 
   * @param portletEditorGUI
   *          the portletEditorGUI.
   */
  public ContainerPortletSettingsPanel(PortletEditorGUI portletEditorGUI)
  {
    super(portletEditorGUI);
  }



  /**
   * Create the specific panel.
   * 
   * @return the specific panel.
   */
  @Override
  public JPanel createSpecificPanel()
  {
    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    this.labelMin = new JLabel(this.getGui().getController().notifyTranslation("com.genia.toolbox.portlet.editor.gui.panel.portlets.ContainerSettingsPanel.Min"));
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 0, 0, 0);
    panel.add(this.labelMin, constraints);

    this.textMin = new JTextField();
    this.textMin.setEditable(false);
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 5, 0, 0);
    panel.add(this.textMin, constraints);

    this.labelMax = new JLabel(this.getGui().getController().notifyTranslation("com.genia.toolbox.portlet.editor.gui.panel.portlets.ContainerSettingsPanel.Max"));
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 0, 0, 0);
    panel.add(this.labelMax, constraints);

    this.textMax = new JTextField();
    this.textMax.setEditable(false);
    constraints.gridx = 2;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 5, 0, 0);
    panel.add(this.textMax, constraints);

    this.labelDirection = new JLabel(this.getGui().getController().notifyTranslation("com.genia.toolbox.portlet.editor.gui.panel.portlets.ContainerSettingsPanel.Direction"));
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 0, 0, 0);
    panel.add(this.labelDirection, constraints);

    this.comboDirection = new JComboBox(LayoutDirection.values());
    this.comboDirection.setEnabled(false);
    this.comboDirection.addActionListener(this);
    constraints.gridx = 2;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 5, 0, 0);
    panel.add(this.comboDirection, constraints);

    constraints.gridx = 1;
    constraints.gridy = 4;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5, 0, 0, 0);
    panel.add(this.createAttributesSettingsPanel(), constraints);

    return panel;
  }



  /**
   * Set the displayed portlet.
   * 
   * @param model
   *          the portlet model.
   */
  @Override
  public void setPortlet(PortletModel model)
  {
    this.removeAllListeners();
    super.setPortlet(model);
    this.showIdentifier();
    ContainerPortletModel containerModel = (ContainerPortletModel) model;
    this.initialiseAttributes(containerModel.getAttributes());
    this.textPortletName.setText(containerModel.getElementName());
    this.areaDescription.setText(this.getGui().getController().notifyTranslation(containerModel.getDescription()));
    this.textMin.setText(containerModel.getMinNbSubPortlets().toString());
    this.textMax.setText(containerModel.getMaxNbSubPortlets().toString());
    this.comboDirection.setSelectedItem(containerModel.getDirection());
    this.addAllListeners();
    this.validateSettings();
  }



  /**
   * The displayed portlet model.
   * 
   * @return the displayed portlet model.
   */
  @Override
  public PortletModel getPortlet()
  {
    ContainerPortletModel updatedModel = (ContainerPortletModel) super.getPortlet();
    updatedModel.setAttributes(this.getUpdatedAttributes());
    return updatedModel;
  }

}
